﻿using System.Linq.Expressions;

namespace SimpleORM
{
    public class ExpressionToSqlSQLServer : ExpressionToSql
    {
        [ExpressionTranslater(MethodName = "Paging")]
        public override void PagingHandler(MethodCallExpression exp)
        {
            NewScript();

            var t = new PagingTranslator();
            t.DataSource = DataSource;
            t.ObjectContextDictionary = ObjectContextDictionary;
            t.FromNameDictionary = FromNameDictionary;
            t.Translate(exp);

            var res = DataSource.CreateScriptFactory().
                                 Paging(GetStack(FROM).Pop(), t.OrderByField, t.Order, t.PageIndex, t.PageSize);

            res = res.Bracket().As(DataSource.CreateScriptFactory().
                                              Constant(FromNameDictionary.Set(ResultObjectContext.ElementType)).
                                              Stress());

            GetStack(FROM).Push(res);
        }
    }
}
